﻿using NAnt.Core;
using NAnt.Core.Attributes;
using System.IO;
using System;
using NAnt.Core.Tasks;

namespace NAnt.SVN.UtilTasks
{
    /// <summary>
    /// Util task to create a temp directory.
    /// Run the deletetempdir task in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("deletetempdir")]
    public class DeleteTempDir : Task
    {
        /// <summary>
        /// Execute the task
        /// </summary>
        protected override void ExecuteTask()
        {
            if (Properties[CreateTempDir.TEMPPROPERTYNAME] != null)
            {
                string tempPath = Properties[CreateTempDir.TEMPPROPERTYNAME];
                try
                {

                    DeleteTask delete = new DeleteTask();
                    this.CopyTo(delete);
                    delete.Directory = new DirectoryInfo(tempPath);
                    delete.IncludeEmptyDirs = true;
                    delete.Execute();

                    Log(Level.Info,
                    string.Format(Resources.Strings.UTIL0102, tempPath));
                }
                catch (Exception e)
                {
                    Log(Level.Error,
                    string.Format(Resources.Strings.UTIL0302, e.Message));
                }
            }
            else
            {
                Log(Level.Warning,
                    string.Format(Resources.Strings.UTIL0305, CreateTempDir.TEMPPROPERTYNAME));
            }
        }
    }
}
